package com.hzlx.service;

import com.hzlx.bean.Product;
import com.hzlx.dao.ProductDao;
import com.hzlx.dto.ProductRequest;
import com.hzlx.dto.ProductResponse;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Component
//@RequiredArgsConstructor
@Slf4j
@Service
public class ProductService {
    @Resource
    private ProductDao productDao;
    public void create(ProductRequest request){
        Product product = Product.builder()
                .name(request.getName())
                .description(request.getDescription())
                .price(request.getPrice())
                .sku(request.getSku())
                .build();
        productDao.save(product);
        log.info("Product {} is saved",product.getId());
    }
    public List<ProductResponse> findAll(){
        return productDao.findAll()
                .stream()
                .map(r->mapToResponse(r))
                .collect(Collectors.toList());
    }
    private ProductResponse mapToResponse(Product model){
        return ProductResponse.builder()
                .id(model.getId())
                .name(model.getName())
                .description(model.getDescription())
                .price(model.getPrice())
                .sku(model.getSku())
                .build();
    }
    public void delete(String id){
        productDao.deleteById(id);
    }
}
